package com.lantu.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lantu.common.vo.RResult;
import com.lantu.sys.entity.Semester;
import com.lantu.sys.service.SemesterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/system/jiaowu")
@CrossOrigin
public class SemesterController {

    @Autowired
    private SemesterService service;
//    分页查询操作
    @GetMapping("/xueqi/{pageNo}/{pageSize}")
    public RResult getAllxueqi(@PathVariable("pageNo") Integer pageNo,
                              @PathVariable("pageSize") Integer pageSize,
                              Semester semester){
        Page<Semester> pageParam = new Page<>(pageNo,pageSize);
        IPage<Semester> semesterByOpr = service.getSemesterByOpr(pageParam, semester);
        return RResult.ok(semesterByOpr);

    }
//    新增或者修改操作
    @PostMapping("/addOrUpdateSemester")
    public RResult addOrUpdateSemester(@RequestBody Semester semester){
        service.saveOrUpdate(semester);
        return RResult.ok();
    }
//    动态下拉框数据
    @GetMapping("/xiala")
    public List<Map<String,Object>> getYear(){
        List<Semester> getlist = service.getlist();
        List<Map<String,Object>> ll = new ArrayList<>();
        for (Semester year: getlist
             ) {
            Map<String,Object> map =new HashMap<>();
            map.put("label",year.getAcademicYear());
            map.put("value",year.getAcademicYear());
            ll.add(map);

        }
        return ll;
    }
//    回显
    @GetMapping("/getid/{id}")
    public RResult<Semester> getBy(@PathVariable("id") Integer id){
        Semester byId = service.getById(id);
        return RResult.ok(byId);

    }
//删除
    @DeleteMapping("/{id}")
    public RResult getById(@PathVariable("id") Integer id){
        service.removeById(id);
        return RResult.ok();
    }


}
